package weaver.formmode.customjavacode.modeexpand.camci;

import weaver.conn.RecordSet;
import weaver.formmode.customjavacode.AbstractModeExpandJavaCodeNew;
import weaver.formmode.customjavacode.modeexpand.camci.utils.CalUtil;
import weaver.formmode.customjavacode.modeexpand.camci.utils.CamciUtil;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-06-13
 * @Description: 敏感信息加密存储
 * @ModifyLog:
 **/

public class splitCkxxInfoModeExpand extends AbstractModeExpandJavaCodeNew {
    @Override
    public Map<String, String> doModeExpand(Map<String, Object> param) {
        Map<String, String> result = new HashMap<String, String>();
        RecordSet rs = new RecordSet();
        try {
            User user = (User) param.get( "user" );
            //数据id
            int billid = -1;
            //模块id
            int modeid = -1;
            //模块id
            int userid = -1;
            RequestInfo requestInfo = (RequestInfo) param.get( "RequestInfo" );
            if (requestInfo != null) {
                billid = Util.getIntValue( requestInfo.getRequestid() );
                modeid = Util.getIntValue( requestInfo.getWorkflowid() );
                userid = Util.getIntValue( user.getUID() );
                if (billid > 0 && modeid > 0) {
                    rs.writeLog( "splitCkxxInfoModeExpand-----------------" + billid );
                    //插入主表
                    rs.executeQuery( "select sjhm from uf_ckxx where id=?", billid );
                    while (rs.next()) {
                        Map<String, String> map = new HashMap<>();

                        String sjhm = Util.null2String( rs.getString( "sjhm" ) );
                        if (!"".equals( sjhm )) {
                            List<String> list = CamciUtil.getListToReverseStr( sjhm );
                            int count = 0;
                            for (String str : list) {
                                Map<String, String> dataMap = CalUtil.divideAndRemainder( str, "3" );
                                for (String key : dataMap.keySet()) {
                                    map.put( "customfield" + count, Util.null2String( key ) );
                                    count++;
                                    map.put( "customfield" + count, Util.null2String( dataMap.get( key ) ) );
                                    count++;
                                }

                            }
                        }
                        map.put( "dataid", billid + "" );
                        rs.executeUpdate("update uf_ckxx set sjhm = '' where id=?",billid);
                        rs.writeLog( ">>>>>map" + map.toString() );
                        if(hasExistData(billid)){
                            CamciUtil.updateModelData( 87,billid,map);
                        }else {
                            CamciUtil.insertModelData( 87, userid, map );
                        }

                    }
                }
            }
        } catch (Exception e) {
            result.put( "errmsg", "自定义出错信息" );
            result.put( "flag", "false" );
        }
        return result;
    }

    public boolean hasExistData(int dataid){
        RecordSet rs = new RecordSet(  );
        rs.executeQuery("select count(id) from uf_mgxx where id=?",dataid);
        rs.next();
        int count = Util.getIntValue(rs.getString( 1 ));
        return count > 0;
    }

}
